package com.ftg.learn.chapter14;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.*;

/**
 * 再做一次转换处理
 * 面向对象  --- 抽象成一个类 ·「变量 | 方法」
 * pojo
 * map.get();
 * 实例.方法();
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/3/7 上午9:57
 */
public class LearnJdbc05 {

    public static void main(String[] args) {
        LearnJdbc05 l = new LearnJdbc05();
//        List<StudentPojo> list = l.queryFoPojo("select sno,sname,ssex,sage from student");
//        for (StudentPojo studentPojo : list) {
//            System.out.println(studentPojo.getSname());
//        }

        List<StudentPojo> list = l.queryFoPojo1(StudentPojo.class, "select sno,sname,ssex,sage from student");
        for (StudentPojo o : list) {
            System.out.println(o.getSname());
        }
    }

    /**
     * 通用性非常差
     * @param sql  sql
     * @param  para para
     * @return List<StudentPojo>
     */
    public List<StudentPojo> queryFoPojo(String sql, Object... para) {
        List<StudentPojo> list = new ArrayList<>();
        try (
                Connection conn = DriverManager.getConnection(JdbcConst.URL, JdbcConst.USERNAME, JdbcConst.PASSWORD);
                PreparedStatement pstm = conn.prepareStatement(sql)
        ) {
            if (Objects.nonNull(para)) {
                for (int i = 0; i < para.length; i++) {
                    pstm.setObject(i + 1, para[i]);
                }
            }
            //处理数据
            ResultSet rs = pstm.executeQuery();
            //不用反射
            ResultSetMetaData metaData = rs.getMetaData();
            while (rs.next()) {
                StudentPojo studentPojo = new StudentPojo();
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    switch (metaData.getColumnName(i + 1)) {
                        case "sno":
                            studentPojo.setSno(rs.getString(i + 1));
                            break;
                        case "sname":
                            studentPojo.setSname(rs.getString(i + 1));
                            break;
                        case "sage":
                            studentPojo.setSage(rs.getInt(i + 1));
                            break;
                        case "ssex":
                            studentPojo.setSsex(rs.getString(i + 1));
                            break;
                    }
                }
                list.add(studentPojo);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    /**
     *
     * @param c    代表你要返回的具体类的关联实例
     * @param sql   sql
     * @param para para
     * @return List<Object>
     */
//    public List<Object> queryFoPojo1(Class c,String sql, Object... para) {
//        List<Object> list = new ArrayList<>();
//        try (
//            Connection conn = DriverManager.getConnection(JdbcConst.URL, JdbcConst.USERNAME, JdbcConst.PASSWORD);
//            PreparedStatement pstm = conn.prepareStatement(sql)
//        ){
//            if (Objects.nonNull(para)) {
//                for (int i = 0; i < para.length; i++) {
//                    pstm.setObject(i + 1, para[i]);
//                }
//            }
//            ResultSet rs = pstm.executeQuery();
//            ResultSetMetaData metaData = rs.getMetaData();
//
//            while(rs.next()){
//
//                Object obj = c.newInstance();
//                for (int i = 0; i < metaData.getColumnCount(); i++) {
//                    PropertyDescriptor  pd = new PropertyDescriptor(metaData.getColumnName(i + 1),c);
//                    Method setMethod = pd.getWriteMethod();
//                    setMethod.invoke(obj,rs.getObject(i + 1));
//                }
//                list.add(obj);
//            }
//
//        } catch (InstantiationException | IllegalAccessException | SQLException | IntrospectionException |
//                 InvocationTargetException e) {
//            throw new RuntimeException(e);
//        }
//        return list;
//    }

    public <T> List<T> queryFoPojo1(Class<T> c,String sql, Object... para) {
        List<T> list = new ArrayList<>();
        try (
                Connection conn = DriverManager.getConnection(JdbcConst.URL, JdbcConst.USERNAME, JdbcConst.PASSWORD);
                PreparedStatement pstm = conn.prepareStatement(sql)
        ){
            if (Objects.nonNull(para)) {
                for (int i = 0; i < para.length; i++) {
                    pstm.setObject(i + 1, para[i]);
                }
            }
            ResultSet rs = pstm.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();

            while(rs.next()){

                Object obj = c.newInstance();
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    PropertyDescriptor  pd = new PropertyDescriptor(metaData.getColumnName(i + 1),c);
                    Method setMethod = pd.getWriteMethod();
                    //switch 判断字段类型 用来做精准存储
                    setMethod.invoke(obj,rs.getObject(i + 1));
                }
                list.add((T)obj);
            }

        } catch (InstantiationException | IllegalAccessException | SQLException | IntrospectionException |
                 InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

}
